var ReportPanel = new Class({
	initialize: function() {
		var reportPanel = new Element('div', {
				'id': 'reportPanel',
				'styles': {
					'height': '25px',
					'width': '65%',
					'float': 'left',
					'margin-left': '20px'
				}
			}),
			reportClick = function(reportCallback) {
				var uri = document.id('reportLocation').value;
				
				// This is how you know the MooTools URI class needs some love...
				if(uri.indexOf('#') == uri.length-1)
					uri = uri.slice(0, uri.length-1);
				if(uri.indexOf(sharpbar.applicationUrl) != 0)
					uri = sharpbar.applicationUrl + uri;

				reportCallback(uri.toString(), parseInt(document.id('depthSelector').value));
			},
			relTypeSelector = new Element('select', {
				'id': 'relationship-type-select'
			});

		reportPanel.grab(((new Element('div', {'text': 'Location: '})).grab(new Element('input', {
			'type': 'text',
			'id': 'reportLocation',
			'value': window.location.href,
			'size': '75'
		}))).grab(new CollectionBrowser(sharpbar.applicationUrl, 'reportLocation')));
		relTypeSelector.grab(new Element('option', {
			'value': 'AllTypes',
			'text': '-- All --',
			'selected': 'selected'
		}));
		sharpbar.relationshipTypeOptions.sort().each(function(type) {
			relTypeSelector.grab(new Element('option', {
				'value': type,
				'text': type
			}));
		});
		reportPanel.grab((new Element('div', { 'text': '*Relationship Type: '})).grab(relTypeSelector));
		reportPanel.grab((new Element('div', { 'text': 'Depth: ' })).grab(new ReportDepthSelector('depthSelector')));
		reportPanel.grab(new Element('br'));
		reportPanel.grab(new Element('input', {
			'type': 'button',
			'value': 'XML Report',
			'events': {
				'click': function() {
					reportClick(function(uri, depth) { 
					 	var itemPath = uri;
						uri += '/report.xml';
						uri = new URI(uri);
						if(itemPath.indexOf('#') == itemPath.length-1)
							itemPath = itemPath.slice(0, itemPath.length-1);
						if(itemPath.indexOf(sharpbar.applicationUrl) == 0)
							itemPath = itemPath.slice(sharpbar.applicationUrl.length);
						if(itemPath.indexOf('/') != 0)
							itemPath = '/' + itemPath;
						
						uri.set('query', 'uri=' + itemPath + '&depth=' + depth);
						window.open(uri); 
					})
				}
			}
		}));
		reportPanel.grab(new Element('input', {
			'type': 'button',
			'value': 'Relationships Report',
			'events': {
				'click': function() {
					var relTypeSelect = document.id('relationship-type-select'),
						relTypeFilter = relTypeSelect[relTypeSelect.selectedIndex].value;
						
					reportClick(function(uri, depth) {
						report(uri, depth, relTypeFilter);
					});
				}
			}
		}));
		reportPanel.grab(new Element('div', { 'text': '*: Relationship Type filter is only applied to Relationship Reports.'}))
		return reportPanel;
	}
});